﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdSkuProduct
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 8:33:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BLogScanner : ILogScanner
{
    // 创建成功
    public int AddLogScanner(long scannerLocId, string productCode, long tireCount)
    {
        var db = Db.Default;
        var result = 0;
        var logScanner = GetLogScannerCreateByProductCode(productCode, tireCount);
        if (logScanner is not null)
        {
            result = UpdateLogScannerCountByProductCode(productCode, tireCount, logScanner.ScannerCount.GetValueOrDefault() + 1);
            return result;
        }

        result = db.LogScanners.Insert(new POLogScanner
        {
            Id = XID.Nextval,
            StartLoc = scannerLocId,
            Barcode = productCode,
            TireCount = tireCount,
        });
        return result;
    }


    // 创建业务后
    public int UpdateLogScannerByProductCode(string productCode, long tireCount, int status, string remarkMemo)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode && t.TireCount == tireCount && t.Status == (int)EnumScannerStatus.Create)
            .Update( new POLogScanner()
            {
                Status = status,
                StatusTime = DateTime.Now,
                RemarkMemo = remarkMemo,
            });
        return db.SaveChanges();
    }

    // 修改扫描数量
    public int UpdateLogScannerCountByProductCode(string productCode, long tireCount, int count)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode && t.TireCount == tireCount && t.Status != (int)EnumScannerStatus.Execute)
            .Update( new POLogScanner()
            {
                ScannerCount = count
            });
        return db.SaveChanges();
    }

    public POLogScanner GetLogScannerByProductCode(string productCode, long tireCount)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode
            && t.TireCount == tireCount
            && t.Status != (int)EnumScannerStatus.Execute
            && t.Status != (int)EnumScannerStatus.Fail
            && t.Status != (int)EnumScannerStatus.Create)
            .FirstOrDefault();
        return result;
    }

    private POLogScanner GetLogScannerCreateByProductCode(string productCode, long tireCount)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode
            && t.TireCount == tireCount
            && t.Status == (int)EnumScannerStatus.Create)
            .FirstOrDefault();
        return result;
    }
    public POLogScanner GetLogScannerNoExecuteByProductCode(string productCode, long tireCount)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode
            && t.TireCount == tireCount
            && t.Status != (int)EnumScannerStatus.Execute)
            .FirstOrDefault();
        return result;
    }



    // 执行成功
    public int UpdateLogScannerSuccessByProductCode(string productCode, long tireCount, int status)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode
            && t.TireCount == tireCount
            && t.Status != (int)EnumScannerStatus.Execute)
            .Update( new POLogScanner()
            {
                Status = status,
                UpdateTime = DateTime.Now,
            });
        return db.SaveChanges();
    }
    // 执行成功
    public int UpdateLogScannerSuccessByProductCodeV2(string productCode, int status)
    {
        var db = Db.Default;
        var result = db.LogScanners
            .Where(t => t.Barcode == productCode
            && t.Status == (int)EnumScannerStatus.Success)
            .Update( new POLogScanner()
            {
                Status = status,
                UpdateTime = DateTime.Now,
            });
        return db.SaveChanges();
    }
}
